<?php
declare (strict_types=1);

namespace app\datacenter\controller;

use app\admin\service\base\AdminService;
use app\BaseController;
use app\datacenter\service\LargeScreenService;
use emqx\Emqx;
use think\facade\Request;
use think\response\Json;

/**
 * 数据中心管理员
 */
class DataAdmin extends BaseController
{

    /**
     * 管理员登录
     * @param AdminService $adminService
     * @return Json
     */
    public function login(AdminService $adminService): Json
    {
        $params['phone'] = Request::param('phone/d', 0);
        $params['password'] = Request::param('password/s', '');
        return $adminService->login($params['phone'], $params['password'], 'datacenter');
    }

    /**
     * 刷新token并获取配置参数
     * @return Json
     */
    public function refreshToken(): Json
    {
        //Emqx对象
        $Emqx = new Emqx();
        $mqtt['host'] = $Emqx->emqx_websocket_host;
        $mqtt['topic_js'] = $Emqx->emqx_datacenter_topic_js;
        $mqtt['username'] = $Emqx->emqx_datacenter_username;
        $mqtt['password'] = $Emqx->emqx_datacenter_password;
        return jsonReturn(1, $mqtt, '刷新成功');
    }

    /**
     * 管理员退出
     * @param AdminService $adminService
     * @return Json
     */
    public function logout(AdminService $adminService): Json
    {
        return $adminService->logout('datacenter');
    }

    /**
     * 获取管理员信息
     * @param AdminService $adminService
     * @return Json
     */
    public function getInfo(AdminService $adminService): Json
    {
        return $adminService->getInfo();
    }

    /**
     * 大屏连接MQTT服务器成功通知
     * @return Json
     */
    public function mqttNotice(): Json
    {
        $LargeScreenService = new LargeScreenService();
        $LargeScreenService->getData(1);
        $LargeScreenService->getData(2);
        $LargeScreenService->getData(3);
        $LargeScreenService->getData(4);
        $LargeScreenService->getData(5);
        return jsonReturn(1, []);
    }

}
